﻿namespace ChinaUnicom.Web
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    public class HttpStatus
    {
        byte[] buffer;

        public HttpStatus(byte[] buffer, WebException ex = null)
        {
            if (buffer == null && ex != null)
            {
                Status = ex.Status;
                Exception = ex;
            }
            else if (buffer == null)
            {
                Status = WebExceptionStatus.UnknownError;
            }
            else
            {
                Status = WebExceptionStatus.Success;
            }

            this.buffer = buffer;
        }

        public WebExceptionStatus Status { get; set; }

        string result = string.Empty;
        public string Result
        {
            get
            {
                if (string.IsNullOrEmpty(result) && buffer != null)
                {
                    result = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                }
                return result;
            }
        }

        Stream responseStream;
        public Stream ResponseStream
        {
            get
            {
                if (responseStream == null && buffer != null)
                {
                    responseStream = new MemoryStream(buffer, 0, buffer.Length, false);
                }
                return responseStream;
            }
        }

        public string RequestUrl { get; set; }

        public Exception Exception { get; set; }
    }
}
